package handler

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"plastic-company/internal/model"
)

// GetDashboardStats 获取仪表板统计数据
func GetDashboardStats(c *gin.Context) {
	var stats model.DashboardStats

	// 获取产品总数
	if err := db.Model(&model.Product{}).Count(&stats.ProductsCount); err != nil {
		c.JSON(http.StatusInternalServerError, model.APIResponse{
			Success: false,
			Message: "获取统计数据失败",
		})
		return
	}

	// 获取应用领域总数
	if err := db.Table("applications").Select("DISTINCT category").Count(&stats.ApplicationsCount); err != nil {
		c.JSON(http.StatusInternalServerError, model.APIResponse{
			Success: false,
			Message: "获取统计数据失败",
		})
		return
	}

	// 获取客户咨询总数
	if err := db.Model(&model.Contact{}).Count(&stats.ContactsCount); err != nil {
		c.JSON(http.StatusInternalServerError, model.APIResponse{
			Success: false,
			Message: "获取统计数据失败",
		})
		return
	}

	// 获取新闻总数
	if err := db.Model(&model.News{}).Count(&stats.NewsCount); err != nil {
		c.JSON(http.StatusInternalServerError, model.APIResponse{
			Success: false,
			Message: "获取统计数据失败",
		})
		return
	}

	c.JSON(http.StatusOK, model.APIResponse{
		Success: true,
		Data:    stats,
	})
}
